<template>
	<view>
		<view class="goods-detail-container" v-if="goodsdetail.length!==0">
			<!-- 轮播图区域 -->
			<swiper indicator-dots autoplay :interval="3000" :duration="1000" circular>
				<swiper-item v-for="(item, i) in goodsdetail.pics" :key="i" @click="previewImg(i)">
					<image :src="item.pics_big"></image>
				</swiper-item>
			</swiper>
			<!-- 商品信息区域 -->
			<view class="goods-info-box">
				<!-- 商品价格 -->
				<view class="price">￥{{goodsdetail.goods_price}}</view>
				<!-- 信息主体区域 -->
				<view class="goods-info-body">
					<!-- 商品名称 -->
					<view class="goods-name">{{goodsdetail.goods_name}}</view>
					<!-- 收藏 -->
					<view class="favi">
						<uni-icons type="star" size="18" color="gray"></uni-icons>
						<text>收藏</text>
					</view>
				</view>
				<!-- 运费 -->
				<view class="yf">快递：免运费</view>
				<rich-text :nodes="goodsdetail.goods_introduce"></rich-text>

			</view>
			<view class="goods_nav">
				<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
					@buttonClick="buttonClick" />
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'
	import cart from '../../store/cart';
	export default {
		data() {
			return {
				goods_id: '',
				goodsdetail: [],
				options: [{
					icon: 'shop',
					text: '店铺',
					// info: 2,
					// infoBackgroundColor: '#007aff',
					infoColor: "red"
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			};
		},
		onLoad(options) {
			this.goods_id = options.goods_id
			this.getGoodsInfo()
			this.options[1].info = this.total
			// this.options[1].info = this.cart.length
		},
		computed: {
			...mapGetters('cart', ['total']),
			...mapState('cart', ['cart'])

		},
		watch: {
			total: {
				deep: true,
				handler(newTotal) {
					this.options[1].info = newTotal
				},
				immediate: true
			}
			// cart() {
			// 	this.options[1].info = this.cart.length
			// }
		},
		methods: {
			...mapMutations('cart', ['addGoodsToCart']),
			onClick(e) {
				if (e.content.text === '购物车') {
					uni.switchTab({
						url: '/pages/car/car'
					})
				}
			},
			buttonClick(e) {
				if (e.content.text === '立即购买') {
					console.log('立即购买');
				} else {
					const goods = {
						goods_id: this.goodsdetail.goods_id, // 商品的Id
						goods_name: this.goodsdetail.goods_name, // 商品的名称
						goods_price: this.goodsdetail.goods_price, // 商品的价格
						goods_count: 1, // 商品的数量
						goods_small_logo: this.goodsdetail.goods_small_logo, // 商品的图片
						goods_state: true // 商品的勾选状态
					}
					this.addGoodsToCart(goods)
					this.options[1].info = this.total
					// uni.switchTab({
					// 	url: '/pages/car/car'
					// })
				}
				// console.log('点击了添加或者购买', e)
				// this.options[2].info++
			},
			async getGoodsInfo() {
				const {
					data
				} = await uni.$http.get('/goods/detail', {
					goods_id: this.goods_id
				})
				data.message.goods_introduce = data.message.goods_introduce.replace(/<img/g,
					'<img style="display:block"').replace(/.webp/g, '.jpg')
				this.goodsdetail = data.message

			},
			previewImg(i) {
				uni.previewImage({
					current: i,
					urls: this.goodsdetail.pics.map(item => item.pics_big)
				})
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		height: 750rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	// 商品信息区域的样式
	.goods-info-box {
		padding: 10px;
		padding-right: 0;

		.price {
			color: #c00000;
			font-size: 18px;
			margin: 10px 0;
		}

		.goods-info-body {
			display: flex;
			justify-content: space-between;

			.goods-name {
				font-size: 13px;
				padding-right: 10px;
			}

			// 收藏区域
			.favi {
				width: 120px;
				font-size: 12px;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				border-left: 1px solid #efefef;
				color: gray;
			}
		}

		// 运费
		.yf {
			margin: 10px 0;
			font-size: 12px;
			color: gray;
		}
	}

	.goods-detail-container {
		// 给页面外层的容器，添加 50px 的内padding，
		// 防止页面内容被底部的商品导航组件遮盖
		padding-bottom: 50px;
	}

	.goods_nav {
		// 为商品导航组件添加固定定位
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}
</style>
